<template>
  <div class="app-container">
    <div class="filter-container">
      订单编号:
      <el-input v-model="listQuery.oid" label="订单编号" placeholder="订单编号" style="width: 200px;" class="filter-item mring10"
        @keyup.enter.native="handleFilter" suffix-icon="el-icon-search"/>
      订单状态:
      <el-select v-model="listQuery.status" clearable placeholder="订单状态" class="filter-item mring10">
        <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter" size="small"> 搜索</el-button>
    </div>
    <div class="mixin-components-container">
      <table class="order-table">
        <thead class="order-th">
          <tr>
            <th width="25%">商品信息</th>
            <th width="13%">价格</th>
            <th width="8%">数量</th>
            <th width="16%">下单人/收货人</th>
            <th width="13%">付款金额</th>
            <th width="8%">订单状态</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody v-for="(row, index2) in list">
          <tr :class="index2 != 0 ? 'title bd' : 'title'">
            <td colspan="4">
              订单编号: {{ row.oid }}
              下单时间:{{ row.create_time | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}
              <el-tag size="small">普通订单</el-tag>
            </td>
            <td colspan="3">付款方式：{{ row.payment_type == 1 ? '管理员支付' : '微信小程序' }}</td>
          </tr>
          <tr v-for="(res, index) in row.goods" class="data-tr">
            <td class="no-border pic">
              <img v-if="res.product_image" :src="res.product_image" class="avatar"
                style="float:left;margin-right:5px;max-width: 80px;max-height: 80px;padding: 1px;border:1px solid #ddd;"></img>
              <p class="goods-name">{{ res.goods_name }}</p>
              <p class="goods-name" v-if="res.spec">规格：{{ res.spec }}</p>
            </td>
            <td>￥{{ res.retail_price }}</td>
            <td>{{ res.num }}</td>
            <td v-if="index == 0" :rowspan="index == 0 ? row.goods.length : 1">
              下单会员：{{ row.username }}<br />收货姓名：{{ row.ship_name }} <br />收货手机：{{ row.ship_mobile }}</td>
            <td v-if="index == 0" :rowspan="index == 0 ? row.goods.length : 1">￥{{ row.price }}</td>
            <td v-if="index == 0" :rowspan="index == 0 ? row.goods.length : 1">{{ row.status_text }}</td>
            <td class="no-border" v-if="index == 0" :rowspan="index == 0 ? row.goods.length : 1">
              <el-button v-if="row.status == 0" size="mini" type="danger" @click="cancelOrder(row.oid)">取消订单</el-button>
              <el-button v-if="row.status == 1" size="mini" type="danger" @click="shipOrder(row.oid)">去发货</el-button>
              <br v-if="row.status < 2" />
              <el-button type="text" @click="orderDetail(row.oid)">详情</el-button>
            </td>
          </tr>
          <tr class="no-content">
            <td colspan="7">&nbsp;</td>
          </tr>
        </tbody>
      </table>

    </div>

    <pagination hide-on-single-page v-show="total > 0" :total="total" :page.sync="listQuery.page"
      :limit.sync="listQuery.size" @pagination="getList" />

  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
import { fetchList, cancelOrder } from '@/api/order/order'

export default {
  name: 'goods',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: '',
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        size: 10,
        page: 1,
        name: null,
      },
      statusOptions: [
        {
          value: 0,
          label: '待付款'
        },
        {
          value: 1,
          label: '待发货'
        },
        {
          value: 2,
          label: '待收货'
        },
        {
          value: 3,
          label: '待评价'
        },
        {
          value: 5,
          label: '已取消'
        },

        {
          value: 6,
          label: '已完成'
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total

        // Just to simulate the time of the request
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {

    },
    orderDetail(oid) {
      this.$router.push({ path: '/order/orderDetail?oid=' + oid })
    },
    handleUpdate(row) {
      this.$alert("功能开发中,敬请期待！")
      //this.$router.push({name:'createGoods','params':{id:row.id}})
    },
    handleDelete(row, index) {
      this.$alert("功能开发中,敬请期待！")
    },
    cancelOrder: function (oid) {
      cancelOrder({ oid: oid }).then(response => {
        //this.listLoading = false
        if (response.data) {
          this.$message('取消成功')
          this.getList()
        }
      })
    },
    shipOrder: function (oid) {
      this.$router.push({ path: '/order/orderShip?oid=' + this.oid })
    }
  },
}
</script>

<style>
.none {
  display: none;
}

.mixin-components-container {
  background-color: #fff;
  margin-top: 12px;
  min-height: calc(100vh - 84px);
  padding: 18px 10px;
  ;
  font-size: 12px;
}

.component-item {
  min-height: 100px;
}

.filter-container {
  padding: 8px 15px;
  background: #fff;
}

.order-table {
  padding-top: 12px;
  border: none;
  border-collapse: collapse;
}

.order-table tr,
.order-table th,
.order-table td,
.order-table tbody {
  border: none;
  padding: 0;
  margin: 0;
}

.order-th {
  background: #f8f8fa;
  margin-top: 30px;
  overflow: hidden;
  line-height: 38px;
  height: 38px;
  font-size: 14px;
  font-weight: 700;
}

.goods-name {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  line-height: 18px
}

.title {
  height: 40px;
  line-height: 40px;
  color: #000
}

.title td {
  padding: 10px 15px;
  color: #000
}

.title.bd td {
  border-top: 1px solid #f8f8f8
}

.data-tr td {
  border-right: 1px solid #f8f8f8;
  text-align: center;
  color: #8c8c8c
}

.data-tr td.pic {
  text-align: left
}

.data-tr td.no-border {
  border: 0
}

.no-content td {
  border-bottom: 1px solid #f8f8f8;
  height: 22px;
  line-height: 22px;
}

.filter-container .el-input,
.filter-container .el-select {
  margin-right: 12px;
}
.filter-container{font-size: 12px;}
.filter-item input[type='text'] {
  height: 32px;
  line-height: 32px;
  font-size: 12px;
}

.mring10 {
  margin-right: 10px;
}
</style>
